import random
import time

harmful_waste = ["废电池", "过期药品", "杀虫喷雾罐", "打印机墨盒", "旧电子产品", "染发剂壳", "日光灯管", "水银体温计",
                 "硒鼓", "除草剂容器", "油漆桶", "灯泡"]
kitchen_waste = ["菜叶", "剩菜", "剩饭", "果皮", "剩下的蛋糕", "鸡心", "鸭血", "落叶", "枯萎的草", "坏掉的树根", "蛋壳",
                 "茶渣", "骨头", "过期的食用油", "落花", "鹅肝", "牛肚", "猪肺", "西瓜皮", "香蕉皮"]
other_waste = ["砖瓦陶瓷", "渣土", "收据发票", "碎陶片", "贝壳", "烟头", "烟灰", "卫生间废纸", "瓷器碎", "照片",
               "一次性餐具", "受污染的玻璃", "受污染的塑料制品", "灰土"]
recoverable_waste = ["衣服", "牛奶纸盒", "报纸", "纸箱子", "泡沫塑料", "矿泉水瓶", "可乐罐", "塑料玩具", "旧毛巾",
                     "玻璃饮料瓶", "镜片", "易拉罐", "金属罐头盒", "金戒指"]
harmful_waste2 = []
kitchen_waste2 = []
other_waste2 = []
recoverable_waste2 = []
all_items = []
all_items.append(harmful_waste)
all_items.append(kitchen_waste)
all_items.append(other_waste)
all_items.append(recoverable_waste)
all_items.append(harmful_waste2)
all_items.append(kitchen_waste2)
all_items.append(other_waste2)
all_items.append(recoverable_waste2)
wrong_list = []

#查找垃圾
def find_rubbish(item: str) -> int:
    """输入：字符串 返回：种类(-1未知 1有害 2厨余 3其他 4可回收)"""
    if (item in harmful_waste) or (item in harmful_waste2):
        return 1
    elif (item in kitchen_waste) or (item in kitchen_waste2):
        return 2
    elif (item in other_waste) or (item in other_waste2):
        return 3
    elif (item in recoverable_waste) or (item in harmful_waste2):
        return 4
    else:
        return -1

#有效输入
def is_valid_input(text):
    # 遍历字符串中的每个字符，检查是否为中文或英文字符
    for char in text:
        if not (('\u4e00' <= char <= '\u9fff') or ('a' <= char <= 'z') or ('A' <= char <= 'Z')) :
            return False
    return True

#显示已分类垃圾
def display_rubbish():
    for i in range(4):
        print("{}有:".format(kindList[i]), end=' ')
        for j in all_items[i]:
            print(j, end=', ')
        print('')
    print('')

#主函数
choice = "menu"
#垃圾类别选项
kindList = ["\033[91m有害垃圾\033[0m", "\033[93m厨余垃圾\033[0m", "\033[94m其他垃圾\033[0m",
            "\033[92m可回收垃圾\033[0m"]
display_rubbish()
shouldInput = True
lj = 0

while True:
    #进入主程序
    if choice == "menu":
        choice = input('''请按照以下提示输入选项: 
                z. 增加垃圾
                d. 删除垃圾
                g. 改变垃圾类别
                c. 查找垃圾
                \n''')
    #垃圾增加阶段
    if choice == "z":
        print("已进入增加垃圾阶段\n")
        if shouldInput:
            lj = input("请输入需要增加的垃圾是：")
            if is_valid_input(lj):
                pass
                Run=is_valid_input(lj)
            else:
                print("请输入中英文！\n")
                Run=is_valid_input(lj)
        else:
            shouldInput = True
        if Run:
            kind = find_rubbish(lj)
            if (kind == 1) or (kind == 2) or (kind == 3) or (kind == 4):
                print("该垃圾已经存在，无需增加! \n")
            else:
                zl = input(
                    "请输入垃圾的分类是(\033[91m有害垃圾\033[0m、\033[93m厨余垃圾\033[0m、\033[94m其他垃圾\033[0m、\033[92m可回收垃圾\033[0m)：")
                if zl == "有害垃圾":
                    harmful_waste.append(lj)
                    print("增加垃圾成功！\n")
                    print("\033[91m有害垃圾\033[0m有：", harmful_waste)
                    print("\n")
                elif zl == "厨余垃圾":
                    kitchen_waste.append(lj)
                    print("增加垃圾成功！\n")
                    print("\033[93m厨余垃圾\033[0m有：", kitchen_waste)
                    print("\n")
                elif zl == "其他垃圾":
                    other_waste.append(lj)
                    print("增加垃圾成功！\n")
                    print("\033[94m其他垃圾\033[0m有：", other_waste)
                    print("\n")
                elif zl == "可回收垃圾":
                    recoverable_waste.append(lj)
                    print("增加垃圾成功！\n")
                    print("\033[92m可回收垃圾\033[0m有：", recoverable_waste)
                    print("\n")
                else:
                    print("\n 增加失败！请按提示选择！\n")
        choice = "menu"
    #垃圾删除阶段
    elif choice == "d":
        print("已进入删除垃圾阶段\n")
        display_rubbish()
        shouldInput = True
        lj = 0
        dl = input("请输入需要删除的垃圾：")
        if is_valid_input(dl):
            pass
            Run = is_valid_input(dl)
        else:
            print("请输入中英文！\n")
            Run = is_valid_input(dl)
        if Run:
            print()
            if dl in harmful_waste:
                harmful_waste.remove(dl)
                print("删除成功！")
                print("\033[91m有害垃圾\033[0m有：", harmful_waste)
                print("\n")
            elif dl in kitchen_waste:
                kitchen_waste.remove(dl)
                print("删除成功！")
                print("\033[93m厨余垃圾\033[0m有：", kitchen_waste)
                print("\n")
            elif dl in other_waste:
                other_waste.remove(dl)
                print("删除成功！")
                print("\033[94m其他垃圾\033[0m有：", other_waste)
                print("\n")
            elif dl in recoverable_waste:
                recoverable_waste.remove(dl)
                print("删除成功！")
                print("\033[92m可回收垃圾\033[0m有：", recoverable_waste)
                print("\n")
            else:
                print("\n 未找到该垃圾，删除失败！\n")
        choice = "menu"
    #垃圾类别改变阶段
    elif choice == "g":
        print("已进入改变垃圾类别阶段\n")
        display_rubbish()
        item = input("输入物品名称 如：菜叶\n>>> ")
        if is_valid_input(item):
            pass
            Run = is_valid_input(item)
        else:
            print("请输入中英文！\n")
            Run = is_valid_input(item)
        if Run:
            kind_ = find_rubbish(item)
            if kind_ == -1:  # 异常情况 没有找到种类
                print("不存在该物品!\n")
            else:
                destination = input(
                    "输入类别：\n类别有：\033[91m有害垃圾\033[0m \033[93m厨余垃圾\033[0m \033[94m其他垃圾\033[0m \033[92m可回收垃圾\033[0m\n>>> ")  # 厨余垃圾
                destination = [destination, 1]
                # 添加到新题库
                passed = True
                if destination[0] == "有害垃圾":
                    kind = 1
                elif destination[0] == "厨余垃圾":
                    kind = 2
                elif destination[0] == "其他垃圾":
                    kind = 3
                elif destination[90] == "可回收垃圾":
                    kind = 4
                else:  # 输入类别未知
                    passed = False
                    print("输入类别错误: {}\n".format(destination[0]))
                if passed:
                    offset = 4 if (destination[1] == '2') else 0
                    all_items[kind - 1 + offset].append(item)
                    print("已将 {} 添加至 {} \n".format(item, destination[0]))
                    # 从原题库中删除
                    if item in all_items[kind_ - 1]:  # 假如这个是基础题库里面的话
                        all_items[kind_ - 1].remove(item)
                    if item in all_items[kind_ - 1 + 4]:  # 假如这个是附加题库里面的话
                        all_items[kind_ - 1].remove(item)
        choice = "menu"
        #垃圾查找阶段
    elif choice == "c":
        print("已进入查找垃圾阶段\n")
        display_rubbish()
        lj = input("输入要查找的垃圾名称：")
        if is_valid_input(lj):
            pass
            Run=is_valid_input(lj)
        else:
            print("请输入中英文！\n")
            Run=is_valid_input(lj)
            choice = "menu"
        if Run:
            kind = find_rubbish(lj)
            if kind == -1:  # 异常情况 没有找到种类
                print("不存在该物品!")
                choice = 'z'
                shouldInput = False
            elif kind == 1:
                print("\033[91m有害垃圾\033[0m\n")
            elif kind == 2:
                print("\033[93m厨余垃圾\033[0m\n")
            elif kind == 3:
                print("\033[94m其他垃圾\033[0m\n")
            elif kind == 4:
                print("\033[92m可回收垃圾\033[0m\n")
            if kind != -1:
                choice = "menu"
    #特殊情况处理
    else:
        print("错误选项，请按照提示重新输入！ヽ(`Д´)ﾉ")
        choice = "menu"
